package com.dqx.opengl_es_demo.model;

import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.text.TextUtils;

import com.dqx.opengl_es_demo.model.objloader.Obj;
import com.dqx.opengl_es_demo.model.objloader.ObjLoaderUtil;
import com.dqx.opengl_es_demo.model.spirit.LeGLBaseSpirit;
import com.dqx.opengl_es_demo.model.spirit.LeGLObjColorSpirit;
import com.dqx.opengl_es_demo.model.spirit.LeGLObjTextureSpirit;
import com.dqx.opengl_es_demo.model.utils.BitmapUtil;
import com.dqx.opengl_es_demo.model.utils.MatrixState;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * 3D模型渲染器
 */
public class ModelRenderer implements GLSurfaceView.Renderer {

    ModelGLSurfaceView surfaceView;

    //模型集合
    List<Obj> objDatas = new ArrayList<>();

    private ArrayList<LeGLBaseSpirit> mObjSprites = new ArrayList<>();


    public ModelRenderer(ModelGLSurfaceView surfaceView) {
        this.surfaceView = surfaceView;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //设置背景色
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        //启用深度测试
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
        //设置为打开背面剪裁
        GLES20.glEnable(GLES20.GL_CULL_FACE);
        //初始化变换矩阵
        MatrixState.setInitStack();
        //初始化定位光光源
        MatrixState.setLightLocation(40, 10, 20);

        //创建模型
        try {
            objDatas = ObjLoaderUtil.load("camaro.obj", surfaceView.getResources());
            if (objDatas != null) {
                for (int i = 0; i < objDatas.size(); i++) {
                    Obj data = objDatas.get(i);
                    //
                    int diffuseColor = data.mtl != null ? data.mtl.Kd : 0xffffffff;
                    float alpha = data.mtl != null ? data.mtl.D : 1.0f;
                    String texturePath = data.mtl != null ? data.mtl.map_Kd : "";
                    // 构造对象
                    if (data.aTexCoords != null && data.aTexCoords.length != 0 && TextUtils.isEmpty(texturePath) == false) {
                        Bitmap bmp = BitmapUtil.getBitmapFromAsset(surfaceView.getResources(), texturePath);
                        LeGLBaseSpirit spirit = new LeGLObjTextureSpirit(surfaceView.getResources(), data.aVertices, data.aNormals, data.aTexCoords, alpha, bmp);
                        mObjSprites.add(spirit);
                    } else {
                        LeGLBaseSpirit spirit = new LeGLObjColorSpirit(surfaceView.getResources(), data.aVertices, data.aNormals, diffuseColor, alpha);
                        mObjSprites.add(spirit);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        //设置视图窗口位置和大小
        GLES20.glViewport(0, 0, width, height);
        //宽高比
        float ratio = (float) width / height;
        //设置相机矩阵
        MatrixState.setCamera(0f, 0f, 0f, 0f, 0f, -1f, 0f, 1f, 0f);

        //设置透视投影矩阵
        MatrixState.setProjectFrustum(-ratio, ratio, -1f, 1f, 2f, 1000f);

        //平移
        MatrixState.translate(0, 0, -10);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //清除深度缓冲与颜色缓冲
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
        //开启混合
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        // 开启平滑阴影，默认是不需要
        gl.glShadeModel(GL10.GL_SMOOTH);

        //MatrixState.pushMatrix();
        //平移
        if (surfaceView.isCanTranslate())
            MatrixState.translate(getTranslateX(), getTranslateY(), 0);

        // 缩放
        if (surfaceView.isCanScal())
            MatrixState.scale(scale, scale, scale);

        // 旋转
        if (surfaceView.isCanRotate()){
            MatrixState.rotate(angleX, 1, 0, 0);
            MatrixState.rotate(angleY, 0, 1, 0);
        }

        // 绘制
        for (int i = 0; i < mObjSprites.size(); i++) {
            LeGLBaseSpirit sprite = mObjSprites.get(i);
            sprite.draw();
        }
        // MatrixState.popMatrix();
    }

    //缩放比例
    private float scale;
    //水平平移增量
    private float translateX;
    //垂直平移增量
    private float translateY;
    //沿X轴旋转角度
    private float angleX;
    //沿Y轴旋转角度
    private float angleY;

    public float getAngleX() {
        return angleX;
    }

    public void setAngleX(float angleX) {
        this.angleX = angleX;
    }

    public float getAngleY() {
        return angleY;
    }

    public void setAngleY(float angleY) {
        this.angleY = angleY;
    }

    public float getScale() {
        return scale;
    }

    public void setScale(float scale) {
        this.scale = scale;
    }

    public float getTranslateX() {
        return translateX;
    }

    public void setTranslateX(float translateX) {
        this.translateX = translateX;
    }

    public float getTranslateY() {
        return translateY;
    }

    public void setTranslateY(float translateY) {
        this.translateY = translateY;
    }
}
